#pragma once
#include "logger.hpp"
#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>

#include <iostream>
#include <unistd.h>
#include <set>
#include <random>
#include <sstream>
#include <atomic>
#include <iomanip>
#include <sys/epoll.h>

enum Error
{
    Success = 0,
    EpollCreate_Error = 1,
    Listensock_Error = 2,
    Bind_Error = 3,
    Listen_Error = 4,

};

enum Code
{
    Error = -1,
    Achieved = 0,
    AlreadyRegistered,
    AlreadyLogged,
    UserDoesNotExist,
    PasswordError,

};



void SetNonBlock(int fd)
{
    int fl = fcntl(fd, F_GETFL);
    if (fl < 0)
    {
        ELOG("get fl failed");
        return;
    }

    int n = fcntl(fd, F_SETFL, fl | O_NONBLOCK);
    if (n < 0)
    {
        ELOG("set fl faild");
        return;
    }
}

const std::string defaulthead = "";

class Uuid
{
public:
    static std::string uuid()
    {
        std::string ret;
        int seed = std::random_device()();
        std::mt19937 generator(seed);
        std::uniform_int_distribution<size_t> distribution(0, 255);
        std::stringstream ss;

        for (int i = 0; i < 8; i++)
        {
            ss << std::setw(2) << std::setfill('0') << std::hex << (distribution(generator));
        }

        static std::atomic<size_t> seq(0);
        for (int i = 0; i < 8; i++)
        {
            ss << std::setw(2) << std::setfill('0') << std::hex << ((seq.load() >> (((7 - i) * 8))) & 0xff);
        }
        ++seq;

        ss >> ret;
        return ret;
    }
};